package com.jia.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.commons.valid.AddGroup;
import com.jia.product.entity.Brand;
import com.jia.product.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 品牌(Brand)表控制层
 *
 * @author makejava
 * @since 2025-07-08 18:15:26
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    /**
     * 服务对象
     */
    @Resource
    private BrandService brandService;

    @GetMapping("/list")
    public List<Brand> list() {
        return brandService.list();
    }

    @GetMapping("/page")
    public Page<Brand> page(@RequestParam(defaultValue = "0", required = false) long current,
                            @RequestParam(defaultValue = "10", required = false) long size,
                            @RequestParam(required = false) String key) {
        return brandService.pageQuery(current, size, key);
    }


    @PostMapping("/batch/insert")
    //@Validated @valid 数组 notBlack 都没有生效
    public String add(@Validated @RequestBody List<Brand> brandList) {
        return "success";
    }

    @PostMapping("/insert")
    //@Validated @valid 实例对象 notBlack 都生效
    // 详细的错误提示 ValidationMessages.properties 中文版 ValidationMessages_zh_CN.properties
    public String add(@Validated({AddGroup.class}) @RequestBody Brand brand) {
        return "success";
    }

    @PostMapping("/add")
    // 初始异常处理
    public String add(@Validated @RequestBody Brand brand, BindingResult result) {
        if (result.hasErrors()) {
            result.getFieldErrors().forEach(error ->
                    System.out.println(error.getField() + ":" + error.getDefaultMessage()));
            return "error";
        }
        return "success";
    }

    @PutMapping("/batch/update")
    public String update(@Valid @RequestBody List<Brand> brandList) {
        return "success";
    }

    @PutMapping("/update")
    public String update(@RequestBody Brand brand) {
        brandService.updateByIdRelation(brand);
        return "success";
    }

}

